package com.oncecloud.devops.client.interceptor;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.oncecloud.devops.client.entity.User;
import com.oncecloud.devops.client.util.Result;
/**
 * 访问拦截器
 * 
 * @author mingwei.dmw
 *
 */
@Repository
public class AccessInterceptor implements HandlerInterceptor {
	private static Logger logger = Logger.getLogger(AccessInterceptor.class);
	private List<String> ignores;
	private List<String> ignoreUrls;
	/**
	 * @return the ignores
	 */
	public List<String> getIgnores() {
		return ignores;
	}

	/**
	 * @param ignores the ignores to set
	 */
	public void setIgnores(List<String> ignores) {
		this.ignores = ignores;
	}

	public void setIgnoreUrls(List<String> ignoreUrls) {
		this.ignoreUrls = ignoreUrls;
	}

	public List<String> getIgnoreUrls() {
		return ignoreUrls;
	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		String requestPath = request.getServletPath();
		System.out.println("===================="+requestPath);
		return true;
//        request.setCharacterEncoding("UTF-8");
//		if (null != ignores) {
//			for (String url : ignores) {
//				if (requestPath.contains(url)) {
//					return true;
//				}
//			}
//		}
//		User user = (User) request.getSession().getAttribute("user");
//		if (null == user) {
//			response.sendRedirect(request.getContextPath() + "/login.html");
//			return false;
//		} else {
//			if (null != ignoreUrls) {
//				for (String url : ignoreUrls) {
//					if (requestPath.contains(url)) {
//						return true;
//					}
//				}
//			}
//			@SuppressWarnings("unchecked")
//			List<String> authlist = (List<String>) request.getSession().getAttribute("authlist");
//			if (!requestPath.equals("/")) {
//				if (!authlist.contains(requestPath.replaceAll("/\\d+", ""))) {
//					logger.warn("该url已被拦截：" + requestPath);
//					Result result = new Result(false, "越权访问！");
//					response.setContentType("application/json;charset=utf-8");
//					response.getWriter().println(JSONObject.toJSONString(result));
//					response.flushBuffer();
//					response.getWriter().close();
////					request.getSession().invalidate();
////					response.sendRedirect(request.getContextPath() + "/login.html");
//					return false;
//				}
//			}
//			return true;
//		}
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
	}

}
